let rotate = 0;
var app = getApp(); 
Page({
  data: {
    inputValue:"",
    picUrl:"http://test-1252734879.costj.myqcloud.com/images/tooopen_sy_196830252422.jpg",
    musicUrl:"http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46",
    author:"许巍",
    name:"此时此刻",
    rotateFlag: false, // 控制专辑图片是否在旋转
    transform:"",//控制旋转的的css属性，默认不旋转
    songsList1:[],//流行榜
    songList2:[],//热歌榜
    songList3:[],//新歌榜
    searchList:[],//搜索歌单
    chooce1:'currentBtn',
    chooce2: '',
    chooce3: '',
    marginLeft:1,
    marginLeftView:1,
    searchFlag:'none',
    searchTitle:'搜到以下歌曲',
    listViewFlag1:'block',
    listViewFlag2:'none'
  },
  onLoad: function () {
    //console.log('onLoad')
    this.requestHotSongs(4);
    
  },
  requestHotSongs:function(hotType){
    var that = this;
    wx.request({
      url: 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg',
      data: {
        g_tk: 5381,
        uin: 0,
        format: 'json',
        inCharset: 'utf-8',
        outCharset: 'utf-8',
        notice: 0,
        platform: 'h5',
        needNewCode: 1,
        tpl: 3,
        page: 'detail',
        type: 'top',
        topid: hotType,
        _: Date.now()
      },
      method: 'GET',
      header: { 'Content-Type': 'application/json' },
      charset: 'UTF-8',
      success: function (res) {
        //console.log(res.data);
        if (res.statusCode == 200) {
          var dataSongs = res.data.songlist;
          var songs = [];//存指定的数量的歌曲
          for (var i = 0; i < app.globalData.songnum; i++) {
            songs[i] = dataSongs[i].data;
            songs[i].albumpic = "http://y.gtimg.cn/music/photo_new/T002R150x150M000" + songs[i].albummid +'.jpg';
          }
          if (songs.length > 0) {
            if(hotType==4){
              that.setData({
                songList1: songs,
                chooce1: 'currentBtn',
                chooce2: '',
                chooce3: '',
                marginLeft:1,
              });
              console.log(that.data.songList1);
            }
            else if (hotType == 26)
            {
              that.setData({
                songList2: songs,
                chooce1: '',
                chooce2: 'currentBtn',
                chooce3: '',
                marginLeft:0,
              })
            }
            else if (hotType == 27)
            {
              that.setData({
                songList3: songs,
                chooce1: '',
                chooce2: '',
                chooce3: 'currentBtn',
                marginLeft:-1
              })
            }
            //每次请求新列表后，都要更新播放列表
            app.globalData.playSongList = songs;
          }
        }
      }
    });
  },
  onReady:function(){
    this.audioCtx  = wx.createAudioContext("myAudio");
  },
  onHind:function(){
  },
  getHitList:function(e){
    var hotType=e.currentTarget.dataset.id;
    if(hotType==4){
      this.requestHotSongs(hotType);
    }
    else if(hotType==26){
      this.requestHotSongs(hotType);
    }
    else {
      this.requestHotSongs(hotType);
    }
  },
  //查询歌曲的输入绑定事件
  bindMusicNameInput:function(event){
    var inputValue = event.detail.value
    this.setData({
      inputValue:inputValue
    })
    return inputValue;
  },
  // 点击搜索事件
  searchMusic:function(){
    var inputValue = this.data.inputValue;
    if(inputValue){
      this.searchMusicHandler(inputValue,1)
    }
  },
  searchMusicHandler: function (keyword, page){
    var that=this;
    wx.request({
      url: 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp',
      data: {
        g_tk: 5381,
        uin: 0,
        format: 'json',
        inCharset: 'utf-8',
        outCharset: 'utf-8',
        notice: 0,
        platform: 'h5',
        needNewCode: 1,
        w: keyword,
        zhidaqu: 1,
        catZhida: 1,
        t: 0,
        flag: 1,
        ie: 'utf-8',
        sem: 1,
        aggr: 0,
        perpage: 20,
        n: 20,
        p: page,
        remoteplace: 'txt.mqq.all',
        _: Date.now()
      },
      method: 'GET',
      header: { 'content-Type': 'application/json' },
      charset:'UTF-8',
      success: function (res) {
        if (res.statusCode == 200) {
          console.log(res.data);
          var searchList=res.data.data.song.list;
          var songs=[];
          if (searchList.length==0)
          {
            console.log("未搜索到相关歌曲!");
            that.setData({
              searchList: songs,
              marginLeftView: 0,
              searchFlag: 'block',
              searchTitle: '未搜到歌曲', 
              listViewFlag1:'none',
              listViewFlag2:'block'
            });
            return;
          }
          
          if (searchList.length > 0) {
            var c = app.globalData.songnum > searchList.length ? searchList.length : app.globalData.songnum;
            for (var i = 0; i < c; i++) {
              songs[i] = searchList[i];
              songs[i].albumpic = "http://y.gtimg.cn/music/photo_new/T002R150x150M000" + songs[i].albummid + '.jpg';
            }
            that.setData({
              searchList: songs,
              marginLeftView:0,
              searchFlag:'block',
              searchTitle:'搜到以下歌曲',
              listViewFlag1: 'none',
              listViewFlag2: 'block'
            });
            //每次请求新列表后，都要更新播放列表
            app.globalData.playSong=0;
            app.globalData.playSongList = songs;
          }
        }
      }
    });
  },
  // 开始音乐--开始旋转
  playMusic:function(){
    this.setData({
      rotateFlag:true
    });
    this.myRotate();
  },
  // 暂停音乐
  pauseMusic:function(){
    this.setData({
      rotateFlag:false
    });
  },
  // 控制专辑图片旋转
  toggleRotate: function() {
    //console.log('旋转控制');
    if (this.data.rotateFlag) {
      this.pauseMusic();
      this.audioCtx.pause();
    } else {
      this.playMusic();
      this.audioCtx.play();
    }
  },
  //旋转函数
  myRotate: function() {
    rotate++;
    let transform = `transform:rotate(${rotate}deg);`; //变换角度
    this.setData({
      transform:transform
    });
    const animation = setTimeout(() => {
      this.myRotate();
    }, 30);
    if (!this.data.rotateFlag) {
      clearTimeout(animation);
    };
  },
  goBackToHotList:function(){
    this.requestHotSongs(4);
    this.setData({
      marginLeftView: 1,
      searchFlag:'none',
      listViewFlag1: 'block',
      listViewFlag2: 'none'
    });
  },
  //点击歌曲进入播放页面
  selectSong:function(e){
    // console.log('原本播放音乐');
    // console.log(app.globalData.playSong);
    var index = e.currentTarget.dataset.index;
    
    if (app.globalData.playSong != index)
    {
      app.globalData.playSong = index;
     // console.log("更改");
      //console.log(app.globalData.playSong);
    }
    // ;
    // console.log(app.globalData.playSong);
    wx.navigateTo({
      url: '../play/play'
    })
  }
})
